import random
import time

import requests
from bs4 import BeautifulSoup

def fetch_douban_movie():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        for s in range (0,250,25) :
            print(f"第{int(s/25 +1)}页数据：")
            url = f'https://movie.douban.com/top250?start={s}'
            response = requests.get(url, headers=headers)

            # 检查响应是否成功，如果响应失败会抛出异常
            response.raise_for_status()

            # 第二个参数：指定使用的解析器，'html.parser'是PYthon自带的HTml解析器
            # soup:代表的是整个解析后的文档树
            soup = BeautifulSoup(response.text, 'html.parser')

            # 找到所有的电影
            items = soup.find_all('div', class_='item')

            for item in items:

                title = item.find('span', class_='title').text
                print(title)

            time.sleep(random.uniform(1, 5))


    except requests.exceptions.RequestException as e:
        print("请求失败",e)
    except Exception as e:
        print(e)



if __name__ == '__main__':
    fetch_douban_movie()